﻿
namespace Leetcode.N0041;

public class Solution
{
    public int FirstMissingPositive(int[] nums)
    {
        for (int i = 0; i < nums.Length; i++)
        {
            while (nums[i] > 0 && nums[i] <= nums.Length && nums[i] != i + 1)
            {
                var moveTo = nums[nums[i] - 1];
                if (moveTo == nums[i])
                {
                    nums[i] = 0;
                    break;
                }

                nums[nums[i] - 1] = nums[i];
                nums[i] = moveTo;
            }
        }
        var ans = nums.Length + 1;

        for (int i = 0; i < nums.Length; i++)
        {
            if (nums[i] != i + 1)
            {
                ans = i + 1;
                break;
            }

        }
        return ans;
    }
}
